﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Common
{
    public class InputManager
    {
        public static InputInstance OwnerInput = new InputInstance();
        public static bool CanInput = true;
        public static float DeltaTime = 0;
        public static void Init()
        {
            //#if WEBPLAYER || 
            //#endif
        }

        #region getRun
        public static float leftClickDuartion = 0, rightClickDuration = 0;
        private static void getRun()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                if (leftClickDuartion < .3f) OwnerInput.Run = true;
                else OwnerInput.Run = false;
                leftClickDuartion = 0;
            }
            else
            {
                leftClickDuartion += DeltaTime;
                if (leftClickDuartion > 20)
                {
                    leftClickDuartion = 0;
                }
            }
            if (Input.GetKeyDown(KeyCode.D))
            {
                if (rightClickDuration < .3f) OwnerInput.Run = true;
                else OwnerInput.Run = false;
                rightClickDuration = 0;
            }
            else
            {
                rightClickDuration += DeltaTime;
                if (rightClickDuration > 20)
                {
                    rightClickDuration = 0;
                }
            }
        }
        #endregion

        #region getMovement
        private static float leftDuration = 0, rightDuration = 0;
        private static void getMovement()
        {
            OwnerInput.MoveLeft = Input.GetKey(KeyCode.A);
            OwnerInput.MoveRight = Input.GetKey(KeyCode.D);
            if (!OwnerInput.MoveLeft && !OwnerInput.MoveRight)
            {
                leftDuration = rightDuration = 0;
                OwnerInput.Run = false;
            }
            else
            {
                if (OwnerInput.MoveLeft) leftDuration += DeltaTime;
                else leftDuration = 0;
                if (OwnerInput.MoveRight) rightDuration += DeltaTime;
                else rightDuration = 0;
                if (leftDuration > 0 && rightDuration > 0)
                {
                    if (leftDuration >= rightDuration)
                    {
                        OwnerInput.MoveLeft = false;
                    }
                    else
                    {
                        OwnerInput.MoveRight = false;
                    }
                }
            }
        }
        #endregion

        #region getJump
        private static void getJump()
        {
            if (!OwnerInput.Jump) OwnerInput.Jump = Input.GetKeyDown(KeyCode.Space);
        }
        #endregion

        #region getFire
        private static float fireDuration = 0;
        private static byte fireCount = 0;
        private static bool fire;
        private static void getFire()
        {
            OwnerInput.Fire1 = Input.GetButtonDown("Fire1");
            //if (OwnerInput.Grounded)
            //{
            //    if (OwnerInput.Run)
            //    {
            //        if (fire) OwnerInput.Fire1 = true;
            //    }
            //    else
            //    {
            //        if (fire)
            //        {
            //            if (fireCount == 0)
            //            {
            //                fireCount++;
            //                OwnerInput.Fire1 = true;
            //            }
            //            else if (fireCount == 1)
            //            {
            //                fireCount++;
            //                if (fireDuration < .3f) OwnerInput.Fire2 = true;
            //            }
            //            else
            //            {
            //                fireCount = 0;
            //                if (fireDuration < .3f) OwnerInput.Fire3 = true;
            //            }
            //            fireDuration = 0;
            //        }
            //        else
            //        {
            //            fireDuration += DeltaTime;
            //            if (fireDuration > .3f)
            //            {
            //                fireDuration = 0;
            //                fireCount = 0;
            //            }
            //        }
            //    }
            //}
            //else
            //{
            //    if (fire)
            //    {
            //        OwnerInput.Fire1 = true;
            //    }
            //}
            OwnerInput.Suck = Input.GetKey(KeyCode.Mouse1);
            if (Input.GetKeyDown(KeyCode.Alpha1)) OwnerInput.Action1 = true;
            if (Input.GetKeyDown(KeyCode.Alpha2)) OwnerInput.Action2 = true;
        }
        #endregion

        public static void Update()
        {
            DeltaTime = Time.deltaTime;
            if (InputManager.CanInput)
            {
                if (OwnerInput.ActionIdle)
                {
                    getJump();
                    getRun();
                    getMovement();
                }
                getFire();
            }
        }
        public static void FixedUpdate()
        {
            DeltaTime = Time.deltaTime;
        }
        public static void LateUpdate()
        {

        }

    }
}
